package com.calque;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.widget.Toast;

import com.commun.Param;
import com.commun.Utils;
import com.commun.Utils.CalqueEnum;
import com.geo.GeoTools;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.object.*;

public class CalqueSitesHavePicture extends ItemizedOverlay<OverlayItemCustom> {

	/* Declration */
	private List<OverlayItemCustom> items = new ArrayList<OverlayItemCustom>();

	private Drawable marker = null;
	private Context context = null;
	OverlayItemCustom overlayItem;
	MapView mapView;
	public static Toast myToast;
	private Handler mHandler = new Handler();
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			Utils.DisparitonLayoutImageTaost(context);
		}
	};

	/* Overlay Methodes */

	private static CalqueSitesHavePicture instance;

	public static CalqueSitesHavePicture getInstance() {

		return instance;
	}

	public static void InitialisationInsatance(Drawable marker,
			Context context, MapView mapView) {

		instance = new CalqueSitesHavePicture(marker, context, mapView);
	}

	private CalqueSitesHavePicture(Drawable marker, Context context,
			MapView mapView) {
		super(marker);
		this.marker = marker;
		this.context = context;
		this.mapView = mapView;
		// DomFeedParser domFeedParser = new DomFeedParser(Param.URI);

		// Renitialise() ;

	}

	public void Renitialise() {
		items = new ArrayList<OverlayItemCustom>();
		for (Mosque mosquai : Utils.getMosquais())
			if (mosquai.getHavePicture()) {
				OverlayItemCustom item = new OverlayItemCustom(
						GeoTools.makeGeoPoint(mosquai.getLatitude(),
								mosquai.getLongitude()), mosquai.getInfo(),
						mosquai.getNom());
				item.setMosquai(mosquai);

				int resID = ((Activity) context).getResources().getIdentifier(
						mosquai.getPictureName(), "drawable",
						((Activity) context).getPackageName());
				item.setResImageMosque(resID);

				items.add(item);

			}
		populate();
	}

	@Override
	protected OverlayItemCustom createItem(int i) {
		return (items.get(i));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, false);
		boundCenterBottom(marker);

		if (!items.isEmpty()
				&& Utils.geoPointsAreSame(items.get(nbTaped).getPoint(),
						mapView.getMapCenter()) && animationLaunched) {

			String text = items.get(nbTaped).getTitle() + " "
					+ items.get(nbTaped).getSnippet();

			Utils.MyCustomisedViewToast(context, text, items.get(nbTaped)
					.getResImageMosque());

			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, Param.LONG_DELAY);

			animationLaunched = false;
			Utils.calqueEnum = CalqueEnum.HAVE_PICTURE_TEXT;
		}

	}

	private boolean animationLaunched = false;

	private int nbTaped;

	@Override
	protected boolean onTap(int i) {

		mapView.getController().animateTo(items.get(i).getPoint());
		animationLaunched = true;
		nbTaped = i;

		return (true);
	}

	@Override
	public int size() {

		return (items.size());
	}

	public void Populate() {
		populate();
	}

}